home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / collectn / ceimpl.jav < prev    next >
Encoding:
Text File  |  1995-10-14  |  2.7 KB  |  106 lines

  1. /*
  2.   File: CEImpl.java
  3.  
  4.   Originally written by Doug Lea and released into the public domain. 
  5.   Thanks for the assistance and support of Sun Microsystems Labs, Agorics 
  6.   Inc, Loral, and everyone contributing, testing, and using this code.
  7.  
  8.   History:
  9.   Date     Who                What
  10.   24Sep95  dl@cs.oswego.edu   Create from collections.java  working file
  11.   13Oct95  dl                 Changed protection statuses
  12.  
  13. */
  14.   
  15. package collections;
  16.  
  17. import java.util.Enumeration;
  18. import java.util.NoSuchElementException;
  19.  
  20. /**
  21.  *
  22.  * A convenient base class for implementations of CollectionEnumeration
  23.  * @author Doug Lea
  24.  * @version 0.93
  25.  *
  26.  * <P> For an introduction to this package see <A HREF="index.html"> Overview </A>.
  27. **/
  28.  
  29. class CEImpl implements CollectionEnumeration {
  30.  
  31. /**
  32.  * The collection being enumerated
  33. **/
  34.  
  35.   protected UpdatableCollection coll_;
  36.  
  37. /**
  38.  * The version number of the collection we got upon construction
  39. **/
  40.  
  41.   protected int version_;
  42.  
  43. /**
  44.  * The number of elements we think we have left.
  45.  * Initialized to coll_.size() upon construction
  46. **/
  47.  
  48.   protected int remaining_;
  49.  
  50.   protected CEImpl(UpdatableCollection c) {
  51.     coll_ = c;
  52.     version_ = c.version();
  53.     remaining_ = c.size();
  54.   }
  55.  
  56. /**
  57.  * Implements collections.CollectionEnumeration.corrupted.
  58.  * Claim corruption if version numbers differ
  59.  * @see collections.CollectionEnumeration#corrupted
  60. **/
  61.  
  62.   public boolean corrupted() { 
  63.     return version_ != coll_.version(); 
  64.   }
  65.  
  66. /**
  67.  * Implements collections.CollectionEnumeration.numberOfRemaingingElements.
  68.  * @see collections.CollectionEnumeration#numberOfRemaingingElements
  69. **/
  70.   public int numberOfRemainingElements() { 
  71.     return remaining_; 
  72.   }
  73.  
  74. /**
  75.  * Implements java.util.Enumeration.hasMoreElements.
  76.  * Return true if numberOfRemainingElements > 0 and not corrupted
  77.  * @see java.util.Enumeration#hasMoreElements
  78. **/
  79.   public boolean hasMoreElements() { 
  80.     return !corrupted() && remaining_ > 0; 
  81.   }
  82.  
  83. /**
  84.  * Subclass utility. 
  85.  * Tries to decrement remaining_, raising exceptions
  86.  * if it is already zero or if corrupted()
  87.  * Always call as the first line of nextElement.
  88. **/ 
  89.   protected void decRemaining() throws NoSuchElementException {
  90.     if (corrupted()) 
  91.       throw new CorruptedEnumerationException(version_, coll_.version(), coll_, "Using version " + version_ + "but now at version " + coll_.version());
  92.     else if (numberOfRemainingElements() <= 0)
  93.       throw new NoSuchElementException("exhausted enumeration");
  94.     else
  95.       --remaining_;
  96.   }
  97.  
  98. /**
  99.  * Implements java.util.Enumeration.nextElement.
  100.  * No-Op default version
  101.  * @see java.util.Enumeration#nextElement
  102. **/
  103.   public Object nextElement() { return null; }
  104. }
  105.  
  106.